//////////////////////////////////////////page_parkIndexGetCar////////////////////
$(document).on("pageInit", "#page_parkIndexGetCar", function(e, pageId, $page) {
    my.givetitle('泊车获取车辆');
    $('.parIndexGetCar-myCar').empty()
    setTimeout(function(){
       $.showPreloader()
    },1)

    key=my.get_key(my.getCookie('token'))
    var getCars=function(cars){
        var list=cars
        var other_name=''
        var other_id=''
        $('.get-car-other').click(function(){
            $('.get-car-listb,.get-car-listc,.get-car-listd').hide()
            $('.get-car-other-box,.get-car-other-btn').show()
            other_name='[name='+$(this).attr('name')+'_name'
            other_id='[name='+$(this).attr('name')+'_id'
        })
        $('.get-car-other-btn').click(function(){
            $('.carInfo-parkIndex').val($('.get-car-other-box').val())
            $(other_name).val($('.get-car-other-box').val())
            $(other_id).val(0)
            $.closeModal('.popup')
            $('.popup-overlay').hide()
        })
        $('.get-car-lista span').click(function(){
            $('.get-car-lista span').removeClass('active')
            $(this).addClass('active')
            var name=$(this).attr('name')
            $('.get-car-listb span').not('.get-car-other').remove()
            $('.get-car-listb').show()
            $('.get-car-listc,.get-car-listd,.get-car-other-box,.get-car-other-btn').hide()
            for( k in list){
                if(list[k].brand_spell!=null&&name.match(list[k].brand_spell.substr(0,1))){
                    var html="<span id='"+k+"'>"+list[k].brand_name+"</span>"
                    $('.get-car-listb').prepend(html)
                }
            }
            $('.get-car-listb span').not('.get-car-other').click(function(){
                $('[name=brand_id]').val($(this).attr('id'))
                $('.get-car-listb span').removeClass('active')
                $(this).addClass('active')
                $('.get-car-listc span').not('.get-car-other').remove()
                $('.get-car-listc').show()
                $('.get-car-listd span').not('.get-car-other').remove()
                $('.get-car-listd').hide()
                var j=$(this).attr('id')
                for(k in list[j].xi){
                    var html="<span id='"+j+"' name='"+k+"' >"+list[j].xi[k].cars_name+"</span>"
                    $('.get-car-listc').prepend(html)
                }
                $('.get-car-listc span').not('.get-car-other').click(function(){
                    $('[name=cars_id]').val($(this).attr('name'))
                    $('.get-car-listc span').removeClass('active')
                    $(this).addClass('active')
                    $('.get-car-listd span').not('.get-car-other').remove()
                    $('.get-car-listd').show()
                    var j=$(this).attr('id')
                    var w=$(this).attr('name')
                    for(k in list[j].xi[w].chexing){
                        var html="<span id='"+k+"'>"+list[j].xi[w].chexing[k].models_name+"</span>"
                        $('.get-car-listd').prepend(html)
                    }
                    $('.get-car-listd span').not('.get-car-other').click(function(){
                        $('[name=models_id]').val($(this).attr('id'))

                        $('.carInfo-parkIndex').val($('.get-car-listb span.active').text()+'-'+$('.get-car-listc span.active').text())
                        $.closeModal('.popup-getCar')
                        $('.popup-overlay').hide()
                    })
                })
            })
        })
    }
    my.getCars(getCars)
    $('.carInfo-parkIndex').off('click').on('click',function(){
        $.popup('.popup-getCar');
        $('span[name=abc]').trigger('click')
    })
    $('.itemLocal-parkIndexGetCar span').off('click').on('click',function(){
        $(this).addClass('active').siblings().removeClass('active')
        $('.numlocal-parkIndex label').text($(this).text())
    })
    $('.numlocal-parkIndex').off('click').on('click',function(){
        $.popup('.popup-carInfo-num');
        //$('.getCarLoacl-parkIndexGetCAr').show()
    })
    $('.n-parkIndexGetCar,.y-parkIndexGetCar').off('click').on('click',function(){
        $.closeModal('.popup')
        $('.popup-overlay,.modal-overlay').remove()
        //$('.getCarLoacl-parkIndexGetCAr').hide()
    })
    $('.carInfo-ok').off('click').on('click',function(){
    	if($('.getcar-pp').val()!='' && $('.getcar-cp').val()!=''){
    		var info={
            'user_id':my.getCookie('id'),
            'car_plate':$('.numlocal-parkIndex label').text()+$('.carNum-parkIndex').val(),
            'brand_id':$('[name=brand_id]').val(),
            'cars_id':$('[name=cars_id]').val(),
            'models_id':$('[name=models_id]').val(),
        }
        console.log(info)
        info=JSON.stringify(info)
        info=my.cryptoASEencrypt(info,key)
        $.ajax({
            url:api.addnewcar,
            type: 'post',
            async:true,
            data:{
                'api_value':info
            },
            error:function(){
                $.hidePreloader()
                $.toast("车辆信息获取失败");
            },
            success:function(data){
                $.hidePreloader()
                if(data.match("err_code")!=null){
                    switch(JSON.parse(data).err_code)
                    {
                        case 40003:
                            my.tokenExpire()
                            break;
                        default:
                            $.toast(JSON.parse(data).err_msg);
                    }
                }else{
                    data=JSON.parse(data)
                    data=JSON.parse(my.cryptoASEdencrypt(data.data,key))
                    console.log(data)
                    var info=$('.carInfo-parkIndex').val()+'/'+$('.numlocal-parkIndex label').text()+$('.carNum-parkIndex').val()
                    $('.select-carInfo-parkIndex').text(info)
                    $('[name=user_carid]').val(data.user_carid)
                    sessionStorage.usercar_id_parkCheckCar=data.user_carid
                    $('[name=park_models_id]').val(data.models_id)
                    sessionStorage.parkChoosePayment_usercar_id=data.user_carid
                    sessionStorage.parking_car_plate=data.car_plate
                    $.router.load("#page_parkIndex",true);

                }
            }
        })
    	}else{
    	     $.alert('请输入完整')
    	}

    })
    //获取车辆信息开始

    var carinfo_park={'u_userid':my.getCookie('id')}
    carinfo_park=JSON.stringify(carinfo_park)
    carinfo_park=my.cryptoASEencrypt(carinfo_park,key).toString()
    $.ajax({
        url:api.usercarlist,
        type: 'post',
        async:true,
        data:{
            'api_value':carinfo_park
        },
        error:function(){
        		 $.hidePreloader()
                 $.toast("车辆信息获取失败");
        },
        success:function(data){
            if(data.match("err_code")!=null){
            	 $.hidePreloader()
                $('.mycarpark').hide()
                switch(JSON.parse(data).err_code)
                {
                    case 40003:
                        my.tokenExpire()
                        break;
//    									case 50250:
//    									    $.alert('请先入驻车辆',function(){
//    									    	$.router.loadPage("carManger.html");
//    									    });
//    									    break;
//    									case 50380:
//    									    $.alert('没有已认证的车辆',function(){
//    									    	$.router.loadPage("home.html");
//    									    });
//    									    break;
                    default:
                        console.log(JSON.parse(data).err_msg);
                }
            }else{

                data=JSON.parse(data)
                data=JSON.parse(my.cryptoASEdencrypt(data.data,key))
                console.log(data)
                $('.mycarpark').show()
                $('.parIndexGetCar-myCar').empty()
                for(y=0;y<data.length;y++){

                	var auditresult_state_dos
                	switch(+data[y].auditresult_state){
                		case 1:

                	    auditresult_state_img='<span class="iconfont icon-dhao"></span>'
                		//auditresult_state_dos='认证通过';
                		break;
                		case 0:
                		//auditresult_state_dos='认证中';
                		auditresult_state_img='<span class="iconfont icon-ddai"></span>'
                		break;
                		case 2:
                		//auditresult_state_dos='认证失败';
                		auditresult_state_img='<span class="iconfont icon-cwu"></span>'
                		break;
                		case 3:
                		//auditresult_state_dos='未认证';
                		auditresult_state_img='<span class="iconfont icon-cwu"></span>'
                		break;
                		default:
                		//auditresult_state_dos='默认';
                		auditresult_state_img='<span class="iconfont icon-cwu"></span>'
                	}
                    $('.parIndexGetCar-myCar').append('<div class="parIndexGetCar-myCar-item" data-models_id="'+data[y].models_id+'" name="'+data[y].usercar_id+'"><div>'+auditresult_state_img+'<span class="iconfont icon-qiche"></span><span class="brand_chinese_name">'+data[y].brand_chinese_name+'-'+data[y].cars_name+'</span><div class="car_plate">'+data[y].car_plate+'</div><span style="float: right;"><span class="iconfont icon-zoud"></span></span><div class="clear"></div></div></div>')
                }
                $('.parIndexGetCar-myCar-item').click(function(){
                    var info=$(this).find('.brand_chinese_name').text()+'-'+$(this).find('.car_plate').text()
                    $('.select-carInfo-parkIndex').text(info)
                    $('[name=park_models_id]').val($(this).attr('data-models_id'))
                    $('[name=user_carid]').val($(this).attr('name'))
                    sessionStorage.usercar_id_parkCheckCar=$(this).attr('name')
                    sessionStorage.parkChoosePayment_usercar_id=$(this).attr('name')
                    sessionStorage.parking_car_plate=$(this).find('.car_plate').text()
                    $.router.load("#page_parkIndex");
                    $('.bar-tab').show()
                })
                $.hidePreloader()
            }
        }
    });
    //获取车辆信息结束
});